<template>
    <div class="news-container">
        <h2 class="news-title">最新新闻</h2>
        <div class="news-list">
            <div v-for="news in newsList" :key="news.id" class="news-item" @click="goToDetail(news.id)">
                <el-image :src="news.cover_img" class="news-image"></el-image>
                <div class="image-mask">
                    <!-- 这里显示新闻标题 -->
                    <p class="news-title-in-mask">{{ news.title }}</p>
                    <div class="news-content">{{ news.content }}</div>
                </div>
            </div>
        </div>
        <el-pagination background layout="prev, pager, next" :total="totalNews" :page-size="pageSize" prev-text="上一页"
            :pager-count="3" next-text="下一页" @current-change="handleCurrentChange" />
    </div>
</template>

<script setup>
import { onMounted, ref, } from 'vue'
import { getNewsList } from '@/api/news.js';
import { useRouter } from 'vue-router'

const pageSize = 6; // 每页显示的新闻数量
const currentPage = ref(1); // 当前页码
const totalNews = ref(0); // 总新闻数量
const newsList = ref([]); // 新闻列表数据
const router = useRouter();

const fetchNews = () => {
    getNewsList({
        per_page: pageSize,
        page: currentPage.value
    }).then(res => {
        console.log(res.data.data);
        newsList.value = res.data.data;
        totalNews.value = res.data.total;
    });
};

onMounted(() => {
    fetchNews();
});

const handleCurrentChange = (newPage) => {
    currentPage.value = newPage;
    fetchNews();
};

const goToDetail = (id) => {
    router.push({ name: 'newsDetail', params: { id: id } })
};

</script>

<style scoped>
.news-image{
    img {
        max-width: 100%;
        height: auto;
    }
}
/* 媒体查询：针对1281px及以上的屏幕宽度 */
@media screen and (min-width: 1025px) {
    .news-item {
        display: flex;
        flex-direction: column;
        align-content: center;
        justify-content: center;
        align-items: flex-start;
        position: relative;
        overflow: hidden;
        transition: all 0.3s ease;
    }

    .image-mask {
        position: absolute;
        left: 0;
        bottom: 0;
        background-color: rgba(0, 0, 0, 0.5);
        color: white;
        display: flex;
        align-items: center;
        padding: 15px;
        width: 100%;
        flex-wrap: wrap;
        align-content: center;
        justify-content: flex-start;
        height: 15px;
    }

    /* 鼠标悬停时的样式变化 */
    .news-item:hover .image-mask {
        height: 100%;
        /* 悬停时遮罩层高度变为100% */
    }

    .news-item:hover .news-content {
        max-height: 60px;
    }

    /* 鼠标移出时的样式还原 */
    .news-item:not(:hover) .el-image {
        transform: scale(1);
        transition: transform 0.3s ease;
    }

    .news-item:not(:hover) .image-mask {
        height: 0;
        transition: height 0.3s ease;
    }

    .news-title-in-mask {
        font-size: 14px;
        font-weight: bold;
        padding-bottom: 10px;
        margin-bottom: 5px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .news-content {
        padding-right: 10px;
        font-size: 14px;
        color: white;
        max-height: 0;
        line-height: 1.5;
        overflow: hidden;
        transition: max-height 0.3s ease;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }

    .news-item:hover .el-image {
        transform: scale(1.2);
        transition: transform 0.3s ease;
    }

    .news-container {
        display: flex;
        flex-direction: column;
        align-content: center;
        justify-content: center;
        align-items: flex-start;
        background-color: rgb(236, 240, 246);
        padding: 0 150px;
        width: 100%;
    }

    .el-pagination {
        margin: 20px auto;
    }

    .news-title {
        font-size: 36px;
        text-align: center;
        line-height: 3;
    }

    .news-list {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 60px;
        border-top: 1px solid #ccc;
        padding-top: 24px;
    }

    .news-info {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        background-color: rgba(0, 0, 0, 0.5);
        color: white;
        padding: 10px;
    }
}

/* 媒体查询：针对768px到1280px之间的屏幕宽度 */
@media screen and (min-width: 768px) and (max-width: 1024px) {
    .news-item {
        display: flex;
        flex-direction: column;
        align-content: center;
        justify-content: center;
        align-items: flex-start;
        position: relative;
        overflow: hidden;
        transition: all 0.3s ease;
    }

    .image-mask {
        position: absolute;
        left: 0;
        bottom: 0;
        background-color: rgba(0, 0, 0, 0.5);
        color: white;
        display: flex;
        align-items: center;
        padding: 15px;
        width: 100%;
        flex-wrap: wrap;
        align-content: center;
        justify-content: flex-start;
        height: 15px;
    }

    /* 鼠标悬停时的样式变化 */
    .news-item:hover .image-mask {
        height: 100%;
        /* 悬停时遮罩层高度变为100% */
    }

    .news-item:hover .news-content {
        max-height: 60px;
    }

    /* 鼠标移出时的样式还原 */
    .news-item:not(:hover) .el-image {
        transform: scale(1);
        transition: transform 0.3s ease;
    }

    .news-item:not(:hover) .image-mask {
        height: 0;
        transition: height 0.3s ease;
    }

    .news-title-in-mask {
        font-size: 14px;
        font-weight: bold;
        padding-bottom: 10px;
        margin-bottom: 5px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .news-content {
        padding-right: 10px;
        font-size: 14px;
        color: white;
        max-height: 0;
        line-height: 1.5;
        overflow: hidden;
        transition: max-height 0.3s ease;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }

    .news-item:hover .el-image {
        transform: scale(1.2);
        transition: transform 0.3s ease;
    }

    .news-container {
        display: flex;
        flex-direction: column;
        align-content: center;
        justify-content: center;
        align-items: flex-start;
        background-color: rgb(236, 240, 246);
        padding: 0 100px;
        width: 100%;
    }

    .el-pagination {
        margin: 20px auto;
    }

    .news-title {
        font-size: 36px;
        text-align: center;
        line-height: 3;
    }

    .news-list {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 20px;
        border-top: 1px solid #ccc;
        padding-top: 20px;
    }

    .news-info {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        background-color: rgba(0, 0, 0, 0.5);
        color: white;
        padding: 10px;
    }
}

/* 媒体查询：针对小于768px的屏幕宽度 */
@media screen and (max-width: 768px) {
    .news-item {
        display: flex;
        flex-direction: column;
        align-content: center;
        justify-content: center;
        align-items: flex-start;
        position: relative;
        overflow: hidden;
        transition: all 0.3s ease;
    }

    .el-image {
        max-width: 100%;
        height: auto;
    }

    .image-mask {
        position: absolute;
        left: 0;
        bottom: 0;
        background-color: rgba(0, 0, 0, 0.5);
        color: white;
        display: flex;
        align-items: center;
        padding: 15px;
        width: 100%;
        flex-wrap: wrap;
        align-content: center;
        justify-content: flex-start;
        height: 15px;
    }

    /* 鼠标悬停时的样式变化 */
    .news-item:hover .image-mask {
        height: 100%;
        /* 悬停时遮罩层高度变为100% */
    }

    .news-item:hover .news-content {
        max-height: 60px;
    }

    /* 鼠标移出时的样式还原 */
    .news-item:not(:hover) .el-image {
        transform: scale(1);
        transition: transform 0.3s ease;
    }

    .news-item:not(:hover) .image-mask {
        height: 0;
        transition: height 0.3s ease;
    }

    .news-title-in-mask {
        font-size: 14px;
        font-weight: bold;
        padding-bottom: 10px;
        margin-bottom: 5px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .news-content {
        padding-right: 10px;
        font-size: 14px;
        color: white;
        max-height: 0;
        line-height: 1.5;
        overflow: hidden;
        transition: max-height 0.3s ease;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }

    .news-item:hover .el-image {
        transform: scale(1.2);
        transition: transform 0.3s ease;
    }

    .news-container {
        display: flex;
        flex-direction: column;
        align-content: center;
        justify-content: center;
        align-items: center;
        background-color: rgb(236, 240, 246);
        padding: 0 20px;
        width: 100%;
    }

    .el-pagination {
        margin: 20px auto;
    }

    .news-list {
        display: grid;
        grid-template-columns: repeat(1, 1fr);
        gap: 26px;
        border-top: 1px solid #ccc;
        padding-top: 11px;
    }

    .news-info {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        background-color: rgba(0, 0, 0, 0.5);
        color: white;
        padding: 10px;
    }

    .news-title{
        font-size: 36px;
        text-align: center;
        line-height: 3;
    }
}
</style>